package com.shop.util;

import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

import java.util.Properties;
import java.util.logging.Logger;

/**
 * @author XCH
 * @date 2025 - 06 - 19
 */
public class EmailUtils {
    private static final Logger logger = Logger.getLogger(EmailUtils.class.getName());

    // 邮件服务器配置（建议后期移到配置文件中）
    private static final String SMTP_HOST = "smtp.qq.com";
    private static final String SMTP_PORT = "587"; // 使用 TLS
    private static final String MAIL_USER = "cunesedevelopers@qq.com";
    private static final String MAIL_PASSWORD = "aysrdmpuhujpbbhc";

    // 统一邮件主题和内容样式
    private static final String EMAIL_SUBJECT = "【京淘商城】验证码通知";
    private static final String EMAIL_CONTENT_TEMPLATE =
    "<div style='font-family: Arial, sans-serif; font-size: 16px; color: #333; max-width: 600px; margin: auto; border: 1px solid #eee; padding: 20px; border-radius: 8px;'>"
        + "<h2 style='color: #2C3E50;'>【京淘商城】验证码通知</h2>"
        + "<p>尊敬的用户，您好！</p>"
        + "<p>您正在执行敏感操作，以下是本次操作的验证码：</p>"
        + "<div style='text-align: center; margin: 20px 0;'>"
            + "<span style='display: inline-block; font-size: 24px; font-weight: bold; color: #E74C3C; letter-spacing: 2px;'>%s</span>"
        + "</div>"
        + "<p style='color: #777;'>请妥善保管此验证码，切勿泄露给他人。该验证码有效期为5分钟，如非本人操作，请忽略此邮件。</p>"
        + "<br>"
        + "<p style='font-size: 12px; color: #aaa;'>© 2025 京淘商城 版权所有</p>"
    + "</div>";



    // 发送邮件方法（统一样式）
    public static void sendEmail(String email, String authCode) throws MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true"); // 启用 TLS
        props.put("mail.smtp.host", SMTP_HOST);
        props.put("mail.smtp.port", SMTP_PORT);

        Session mailSession = Session.getInstance(props, new jakarta.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MAIL_USER, MAIL_PASSWORD);
            }
        });

        try {
            MimeMessage message = new MimeMessage(mailSession);
            InternetAddress from = new InternetAddress(MAIL_USER);
            message.setFrom(from);

            InternetAddress to = new InternetAddress(email);
            message.setRecipient(Message.RecipientType.TO, to);

            // 设置统一的主题和内容
            message.setSubject(EMAIL_SUBJECT);
            message.setContent(String.format(EMAIL_CONTENT_TEMPLATE, authCode), "text/html;charset=UTF-8");

            Transport.send(message);
            System.out.println("验证码："+authCode);
            logger.info("邮件发送成功！");
        } catch (MessagingException e) {
            logger.severe("邮件发送失败: " + e.getMessage());
            throw e;
        }
    }
}
